package com.cf.controller;

import com.cf.entity.Result;
import com.cf.entity.Reward;
import com.cf.service.RewardService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class RewardController {
    @Autowired
    private RewardService rewardservice;


    @RequestMapping("/selectReward")
    public Object selectReward(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "2") int pageSize){
        PageInfo<Reward> pageInfo = rewardservice.selectReward(pageNum, pageSize);
        if (pageInfo.getList().size()!=0){
            return new Result(200,"查询成功",pageInfo);
        }
        return new Result(400,"没有数据",null);
    }

    @RequestMapping("/insertReward")
    public Object insertReward(Reward reward){
        int i = rewardservice.insertReward(reward);
        if(i!=0){
            return new Result(200,"添加成功",0);
        }
        return new Result(400,"添加失败",0);
    }

    @RequestMapping("/deleteReward")
    public Object deleteReward(int rewardid){
        int i = rewardservice.deleteReward(rewardid);
        if(i!=0){
            return new Result(200,"删除成功",0);
        }
        return new Result(400,"删除失败",0);
    }

    @RequestMapping("/upadteReward")
    public Object upadteReward(Reward reward){
        int i = rewardservice.updateReward(reward);
        if(i!=0){
            return new Result(200,"修改成功",0);
        }
        return new Result(400,"修改失败",0);
    }

    @RequestMapping("/selectByItemid")
    public Object selectByItemid(@RequestParam(defaultValue = "1") int pagenum, int pagesize,int iid){
        PageInfo<Reward> pageInfo = rewardservice.selectByItemid(iid,pagenum, pagesize);
        if (pageInfo.getList().size()!=0){
            return new Result(200,"查询成功",pageInfo);
        }
        return new Result(400,"没有数据",null);
    }

    @RequestMapping("/selectByrewardid")
    public Object selectByrewardid(int rewardid){
        Reward reward = rewardservice.selectByrewardid(rewardid);
        if (reward!=null){
            return new Result(200,"查询成功",reward);
        }
        return new Result(400,"没有数据",null);
    }

    @RequestMapping("/selectBymoney")
    public Object selectBymoney(int iid,int paymoney){
        int i = rewardservice.selectBymoney(iid,paymoney);
        if (i==0){
            return new Result(200,"可以添加数据",null);
        }
        return new Result(400,"此金额已经拥有",null);
    }

    @RequestMapping("/deleteByIid")
    public Object deleteByIid(int iid){
        int i = rewardservice.deleteByIid(iid);
        if(i!=0){
            return new Result(200,"删除成功",0);
        }
        return new Result(400,"删除失败",0);
    }
}
